﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.Model.Group;
using YouHP.BLL;
using System.Web;

namespace YouHP.Pages.User.Group
{
    public class GroupJoinApply:UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();

            if (ErrorCount > 0) return;

            HaddleCommon();
            if (ErrorCount > 0) return;

            if (IsPost)
                HaddlePost();
            else
                HaddleGet();
        }

        private void HaddlePost()
        {
            int uid = RequestTools.GetFormInt("userid", 0);
            int gid = RequestTools.GetFormInt("groupid", 0);
            int actiontype = RequestTools.GetFormInt("actiontype", -1);

            bool succ=false;
            switch (actiontype)
            {
                case Constants.Received:
                    succ = GroupUserBLL.Instance.UpdateGroupUserRole(gid, uid, Constants.RoleMember);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功！");
                        this.SetUrl("/User/Group/GroupJoinApply.aspx?uid="+UserData.Id);
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败！");
                        return;
                    }
                    break;
                case Constants.Rejected:
                    succ = GroupUserBLL.Instance.UpdateGroupUserRole(gid, uid, Constants.RoleIngore);
                    if (succ)
                    {
                        this.AddMsgLine("操作成功！");
                        this.SetUrl("/User/Group/GroupJoinApply.aspx?uid=" + UserData.Id);
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败！");
                        return;
                    }
                    break;
            }


        }
        protected List<GroupUserInfo> ApplyDataList;
        protected int pageSize = 10;
        protected int pageIndex = 1;
        protected int pageCount = 0;
        protected int dataCount;
        protected string PageNumberString;

        private void HaddleGet()
        {
            int uid = RequestTools.GetQueryInt("uid",0);

            pageIndex = RequestTools.GetQueryInt("page", 1);

            ApplyDataList = GroupUserBLL.Instance.GetGroupJoinApplyList(uid, pageIndex, pageSize, out dataCount);

            pageCount = Utils.GetPageCount(dataCount, pageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PageNumberString = this.GetPagingString(pageIndex, pageCount,
                "/User/Group/GroupJoinApply.aspx" + query, "page");

        }

        private void HaddleCommon()
        {
            this.AddLocation("圈子加入申请", "/User/Group/GroupJoinApply.aspx?uid="+UserData.Id);
            this.SetTitle("圈子加入申请");
        }
    }
}
